//
//  IOSAnalysisClient.m
//  Patrol
//
//  Created by Lee on 13-3-21.
//  Copyright (c) 2013年 fanwu. All rights reserved.
//
#define kAppKey @"Patrol"
#define kServer @"http://61.154.10.49:8096/AppAnalyse/fsti/"
#define kAppLaunchServiceURL @"launch.do"
#define kOperationServiceURL @"action.do"


#import "IOSAnalysisClient.h"
#import "UIDevice+IdentifierAddition.h"
#import "UIDevice-Hardware.h"
#import "FWAppDelegate.h"
#import "AFNetworking.h"
@interface IOSAnalysisClient()
+ (NSString *) deviceID;
+ (NSString *) iosVerison;
+ (NSString *) mobileType;
+ (NSString *) userID;
@end

@implementation IOSAnalysisClient
+ (NSString * )deviceID
{
    static NSString * _deviceID = nil;
    if (!_deviceID) {
        _deviceID = [[UIDevice currentDevice] uniqueGlobalDeviceIdentifier];
    }
    return _deviceID;
}

+ (NSString *) iosVerison{
    static NSString *_iosVersion = nil;
    if (!_iosVersion) {
        _iosVersion = [[UIDevice currentDevice] systemVersion];
    }
    return _iosVersion;
}

+ (NSString *) mobileType{
    static NSString *_iosVersion = nil;
    if (!_iosVersion) {
        _iosVersion = [[UIDevice currentDevice] platformString];
    }
    return _iosVersion;
}


+ (NSString *)userID
{
    static NSString * _uid = nil;
    if (!_uid) {
        //        FWAppDelegate * delegate = (FWAppDelegate *)[UIApplication sharedApplication].delegate;
        //        _uid = delegate.staffInfo.staffId;
        _uid = @"";
    }
    return _uid;
}

+ (NSDictionary * )params : (BOOL)isLogin
{
    static NSDictionary * _params = nil;
    if (!_params) {
        if (isLogin) {
            _params = @{
                        @"device_id" : [IOSAnalysisClient deviceID],
                        @"uid" : [IOSAnalysisClient userID],
                        @"item_code" : kAppKey,
                        @"ios_version" : [IOSAnalysisClient iosVerison],
                        @"mobile_type" : [IOSAnalysisClient mobileType]
                        };
        }else{
            _params = @{
                        @"device_id" : [IOSAnalysisClient deviceID],
                        @"uid" : [IOSAnalysisClient userID],
                        @"item_code" : kAppKey,
                        };
        }
        
    }
    return _params;
}

+ (IOSAnalysisClient *)sharedInstance
{
    static IOSAnalysisClient * _sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[IOSAnalysisClient alloc]initWithBaseURL:[NSURL URLWithString:kServer]];
    });
    return _sharedClient;
}

//#pragma mark - 覆盖基类的实现
//- (id) initWithBaseURL:(NSURL *)url
//{
//    self = [super initWithBaseURL:url];
//    if (!self) {
//        return  nil;
//    }
//    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
//    return self;
//}


- (void)afterLogin
{
    NSDictionary * params = [IOSAnalysisClient params: YES];
    [self postPath:kAppLaunchServiceURL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        DLog(@"After Login Success");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        DLog(@"After Login Failure");
    }];
}

- (void)afterOperation:(NSDictionary *)operationInfo
{
    NSDictionary * params = [IOSAnalysisClient params: NO];
    NSMutableDictionary * requestParams = [NSMutableDictionary dictionaryWithDictionary:params];
    [requestParams addEntriesFromDictionary:operationInfo];
    
    //post data to server
    [self postPath:kOperationServiceURL parameters:requestParams success:^(AFHTTPRequestOperation *operation, id responseObject) {
        DLog(@"After Operation Success");
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        DLog(@"After Operation Failure");
    }];
}


@end
